using ERPBackend.Models;
using ERPBackend.Services;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace ERPBackend.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class SalesOrdersController : ControllerBase
    {
        private readonly ISalesOrderService _salesOrderService;

        public SalesOrdersController(ISalesOrderService salesOrderService)
        {
            _salesOrderService = salesOrderService;
        }

        // GET: api/SalesOrders
        [HttpGet]
        public async Task<ActionResult<IEnumerable<SalesOrder>>> GetSalesOrders()
        {
            var salesOrders = await _salesOrderService.GetAllSalesOrdersAsync();
            return Ok(salesOrders);
        }

        // GET: api/SalesOrders/5
        [HttpGet("{id}")]
        public async Task<ActionResult<SalesOrder>> GetSalesOrder(int id)
        {
            var salesOrder = await _salesOrderService.GetSalesOrderByIdAsync(id);
            if (salesOrder == null)
            {
                return NotFound();
            }
            return Ok(salesOrder);
        }

        // POST: api/SalesOrders
        [HttpPost]
        public async Task<ActionResult<SalesOrder>> PostSalesOrder(SalesOrder salesOrder)
        {
            try
            {
                var createdSalesOrder = await _salesOrderService.CreateSalesOrderAsync(salesOrder);
                return CreatedAtAction("GetSalesOrder", new { id = createdSalesOrder.Id }, createdSalesOrder);
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }
        }

        // PUT: api/SalesOrders/5
        [HttpPut("{id}")]
        public async Task<IActionResult> PutSalesOrder(int id, SalesOrder salesOrder)
        {
            if (id != salesOrder.Id)
            {
                return BadRequest("ID不匹配");
            }

            try
            {
                var result = await _salesOrderService.UpdateSalesOrderAsync(salesOrder);
                if (!result)
                {
                    return NotFound();
                }
                return NoContent();
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }
        }

        // PUT: api/SalesOrders/5/approve
        [HttpPut("{id}/approve")]
        public async Task<IActionResult> ApproveSalesOrder(int id, [FromQuery] int approvedBy)
        {
            try
            {
                var result = await _salesOrderService.ApproveSalesOrderAsync(id, approvedBy);
                if (!result)
                {
                    return NotFound();
                }
                return NoContent();
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }
        }

        // PUT: api/SalesOrders/5/close
        [HttpPut("{id}/close")]
        public async Task<IActionResult> CloseSalesOrder(int id, [FromQuery] string reason, [FromQuery] int closedBy)
        {
            try
            {
                var result = await _salesOrderService.CloseSalesOrderAsync(id, closedBy, reason);
                if (!result)
                {
                    return NotFound();
                }
                return NoContent();
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }
        }

        // DELETE: api/SalesOrders/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteSalesOrder(int id)
        {
            try
            {
                var result = await _salesOrderService.DeleteSalesOrderAsync(id);
                if (!result)
                {
                    return NotFound();
                }
                return NoContent();
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }
        }

        // GET: api/SalesOrders/search?keyword=xxx&customerId=xxx&status=xxx
        [HttpGet("search")]
        public async Task<ActionResult<IEnumerable<SalesOrder>>> SearchSalesOrders(
            [FromQuery] string keyword = null,
            [FromQuery] int? customerId = null,
            [FromQuery] int? status = null)
        {
            var salesOrders = await _salesOrderService.SearchSalesOrdersAsync(keyword, customerId, status);
            return Ok(salesOrders);
        }
    }
}